#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>

#define WRITE_TO_FILE -1
#define READ_FROM_FILE 0

using namespace std;

string itoa (int n) {

	char * s = new char[17];
	string u;

	if (n < 0) { //turns n positive
		n = (-1 * n); 
		u = "-"; //adds '-' on result string
	}

	int i=0; //s counter
  
	do {
		s[i++]= n%10 + '0'; //conversion of each digit of n to char
		n -= n%10; //update n value
	}

	while ((n /= 10) > 0);

	for (int j = i-1; j >= 0; j--) { 
		u += s[j]; //building our string number
	}

	delete[] s; //free-up the memory!
	return u;
}

int nextnum(int mode,int num)
{
	switch(mode)
{
	case WRITE_TO_FILE:
	{
		ofstream fout("nextnum");
		if(!fout){cout<<"Can't write to file!\n";return -1;}		
		fout<<num;
		fout.close();
		break;
	}
	case READ_FROM_FILE:
	if(num==0){
		char buf[17];
		ifstream fin("nextnum");
		if(!fin)
		{cout<<"You want to read next number from file, but it doesn't exist!\nPlease enter correct next number!"<<endl;
		return -1;
		}
		fin>>buf;
		fin.close();
		num=atoi(buf);
		break;
	}
}
return num;
}

int parse(char* cnum,char* fname,char* foutname)
{
	int num=nextnum(READ_FROM_FILE,atoi(cnum));
//cout<<num;
	if(num==-1)return 1;
//cout<<"22";
	string start("<table width=\"400\" cellspacing=\"0\" cellpadding=\"3\" \
bordercolor=\"#111111\" border=\"1\" style=\"border-collapse: collapse;\">\n\
<tbody>\n\
<tr>\n\
<td width=\"80%\" height=\"25\" bgcolor=\"#2c7ada\" align=\"center\"><font color=\"#ffffff\">\
<strong>Наименование товара</strong></font></td>\n\
<td width=\"10%\" height=\"25\" bgcolor=\"#2c7ada\" align=\"center\"><font color=\"#ffffff\">\
<strong>Кол-во</strong></font></td>\
<td width=\"10%\" height=\"25\" bgcolor=\"#2c7ada\" align=\"center\"><font color=\"#ffffff\">\
<strong></strong></font></td>\
</tr>");
/*
<tr> 
<form method="post" action="">
    <input type="hidden" name="my-item-id" value="1" />
    <input type="hidden" name="my-item-name" value="Головка КТ 41-36" />
    <input type="hidden" name="my-item-price" value="1.00" />
<td width="80%" height="20" align="left">Головка КТ 41-36</td> 
<td width="10%" height="20" align="left"><input type="text" name="my-item-qty" value="1" size="3" /></td>
<td width="10%" height="20" align="left"><input type="submit" name="my-add-button" value="+" class="button" /></td>
</form>
</tr>
*/ 
	string end("</tbody>\n</table>");
	string prefix1("<tr>\n<form method=\"post\" action=\"\">\n\
<input type=\"hidden\" name=\"my-item-id\" value=\"");
//<td width=\"100%\" height=\"20\" align=\"left\">");
	string prefix2("\" />\n<input type=\"hidden\" name=\"my-item-name\" value=\"");
	string prefix3("\" />\n<input type=\"hidden\" name=\"my-item-price\" value=\"1.00\" />\n\
<td width=\"80%\" height=\"20\" align=\"left\">");
	string postfix("</td>\n\
<td width=\"10%\" height=\"20\" align=\"left\">\
<input type=\"text\" name=\"my-item-qty\" value=\"1\" size=\"3\" /></td>\
<td width=\"10%\" height=\"20\" align=\"left\">\
<input type=\"submit\" name=\"my-add-button\" value=\"+\" class=\"button\" /></td>\
</form>\
</tr>");
	ifstream fin(fname);
	if(!fin){
		cout<<"Can't open file"<<endl;
		return 1;
	}
	if(!foutname){
		foutname=new char[12];
		strcpy(foutname,"out.txt");
	}
	//else 
	ofstream fout(foutname);
	//string s,t;
	char s[256];
	fout<<start<<endl;
	while(!fin.eof())	
	{
		//fin>>s;
		//fin>>t;s=s+" "+t;
		//fin>>t;s=s+" "+t;
		
		fin.getline(s,256,'\n');
		if(!strcmp(s,"")){
			if(fin.eof())break;
			fout<<end<<endl<<endl<<endl<<start<<endl;		
			continue;
		}
		//cout<<prefix+s+postfix<<endl;
		//fout<<prefix+s+postfix<<endl;
		fout<<prefix1+itoa(num)+prefix2+s+prefix3+s+postfix<<endl;
		num++;
	}
	fout<<end<<endl;
	fin.close();
	fout.close();
	nextnum(WRITE_TO_FILE,num);
	cout<<"All done!\nNext num: "<<num<<endl;
	return 0;
}

void usage(char* name)
{
cout<<"Usage:\n"<<name<<" i IN [OUT]\n\
i\tStart number of jcart forms\
IN\tInput txt file\n\
[OUT]\tOutput HTML page\n\
\tIf not defined, output to out.txt\n";
return;
}

int main(int argc,char* argv[])
{
	switch(argc)
	{
	//case 1:cout<<argv[0]<<endl;break;
	case 3:parse(argv[1],argv[2],0);break;
	case 4:parse(argv[1],argv[2],argv[3]);break;
	default:usage(argv[0]);
	}
	return 0;
}
